package com.ruoyi.standingbook.sanitationvehicle.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.standingbook.sanitationvehicle.domain.bo.SanitationVehicleImportBaseBo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleImportBaseVo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleImportDetailVo;
import com.ruoyi.standingbook.sanitationvehicle.service.ISanitationVehicleImportBaseService;
import com.ruoyi.standingbook.sanitationvehicle.service.impl.SanitationVehicleExcelService;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 环卫作业车导入
 *
 * @author lq
 * @date 2024-01-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/standingbook/sanitationVehicleImpBase")
public class SanitationVehicleImpBaseController extends BaseController {

    private final ISanitationVehicleImportBaseService sanitationVehicleImportBaseService;
    private final SanitationVehicleExcelService sanitationVehicleExcelService;
    
    /**
     * 查询环卫作业车导入列表
     */
    @SaCheckPermission("standingbook:sanitationVehicleImportBase:list")
    @GetMapping("/list")
    public TableDataInfo<SanitationVehicleImportBaseVo> list(SanitationVehicleImportBaseBo bo, PageQuery pageQuery) {
        return sanitationVehicleImportBaseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出环卫作业车导入列表
     */
    @SaCheckPermission("standingbook:sanitationVehicleImportBase:export")
    @Log(title = "环卫作业车导入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SanitationVehicleImportBaseBo bo, HttpServletResponse response) {
        List<SanitationVehicleImportBaseVo> list = sanitationVehicleImportBaseService.queryList(bo);
        ExcelUtil.exportExcel(list, "环卫作业车导入", SanitationVehicleImportBaseVo.class, response);
    }

    /**
     * 获取环卫作业车导入详细信息
     *
     * @param batchNo 主键
     */
    @SaCheckPermission("standingbook:sanitationVehicleImportBase:query")
    @GetMapping("/{batchNo}")
    public R<SanitationVehicleImportBaseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String batchNo) {
        return R.ok(sanitationVehicleImportBaseService.queryById(batchNo));
    }

    /**
     * 新增环卫作业车导入
     */
    @SaCheckPermission("standingbook:sanitationVehicleImportBase:add")
    @Log(title = "环卫作业车导入", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SanitationVehicleImportBaseBo bo) {
        return toAjax(sanitationVehicleImportBaseService.insertByBo(bo));
    }

    /**
     * 修改公园管养导入
     */
    @SaCheckPermission("standingbook:sanitationVehicleImportBase:edit")
    @Log(title = "环卫作业车导入", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SanitationVehicleImportBaseBo bo) {
        return toAjax(sanitationVehicleImportBaseService.updateByBo(bo));
    }

    /**
     * 删除环卫作业车导入
     *
     * @param batchNos 主键串
     */
    @SaCheckPermission("standingbook:sanitationVehicleImportBase:remove")
    @Log(title = "环卫作业车导入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{batchNos}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] batchNos) {
        return toAjax(sanitationVehicleImportBaseService.deleteWithValidByIds(Arrays.asList(batchNos), true));
    }

    /**
     * 新增导入主
     */
    @SaCheckPermission("standingbook:sanitationVehicleImportBase:add")
    @Log(title = "环卫作业车导入", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping("/importData")
    public R<List<String>> importData(@Validated(AddGroup.class) @RequestBody SanitationVehicleImportBaseBo bo) {        
        List<String> list = sanitationVehicleExcelService.importExcel(bo);

        if (list == null) {
            return R.fail();
        } else {
            return R.ok(list);
        }
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "环卫作业车数据", SanitationVehicleImportDetailVo.class, response);
    }
}
